/*****************************************************************************
 * $LastChangedDate: 2009-11-22 22:39:11 -0500 (Sun, 22 Nov 2009) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   HUD (2D box).
 *//*
 * LEGAL:   COPYRIGHT (C) 2008 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#define HUD_HUD_BOX_CC 1
#include "base/module.hh"
using namespace base;
#include "gfx/module.hh"
#include "gfx/osg.hh"
#include "gfx/gfx.hh"
using namespace gfx;
#include "graph/module.hh"
using namespace graph;
#include "view/module.hh"
#include "view/view.hh"
using namespace view;
#include "hud/module.hh"
#include "hud/hud_box.hh"

namespace hud {

////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////  HudBox  ////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

/*****************************************************************************
 * ctor/dtor.
 *****************************************************************************/
HudBox::HudBox( const Rect<int>& rect, const RGBA color )
:   Parent(),
    mRect(rect),
    mColor(color),
    m3D(),
    mDrawX(false)
{
    SET_TYPESIG(this,TYPESIG_HUD_BOX);
}

HudBox::~HudBox()
{
    INVALIDATE_TYPESIG(this,TYPESIG_HUD_BOX);
}

/*****************************************************************************
 * Define box in 2D terms.
 *****************************************************************************/
void
HudBox::Set2D( const Rect<int>& rect )
{
CHECK_TYPESIG(this,TYPESIG_HUD_BOX);

    mRect = rect;

    // Disable 3D transformation.
    m3D.mValid = false;
}

/*****************************************************************************
 * Define box in 3D terms.
 *****************************************************************************/
void
HudBox::Set3D( const WorldVertex& pos3, const uint width2, const uint height2 )
{
CHECK_TYPESIG(this,TYPESIG_HUD_BOX);

    // Enable 3D transformation.
    m3D.mValid   = true;
    m3D.mPos3    = pos3;
    m3D.mWidth2  = width2;
    m3D.mHeight2 = height2;
}

/*****************************************************************************
 * Draw box.
 *****************************************************************************/
void
HudBox::Update( View& view )
{
CHECK_TYPESIG(this,TYPESIG_HUD_BOX);

    // Optionally, the 2D box can be defined in terms of 3D,
    // in order to enclose a 3D object in world space.
    // It computes a 2D rectangle in terms of a 3D position and 2D width,height.
    if ( m3D.mValid )
    {
        if ( view.IF_Z_FACING( m3D.mPos3 ) )
        {
            // Convert to Rect<int>
            const Vector2 center2 = view.Project( m3D.mPos3 );  // transform 3D --> 2D
            mRect.x1 = uint( center2[XX] - (m3D.mWidth2  >> 1) );
            mRect.y1 = uint( center2[YY] - (m3D.mHeight2 >> 1) );
            mRect.x2 = uint( center2[XX] + (m3D.mWidth2  >> 1) );
            mRect.y2 = uint( center2[YY] + (m3D.mHeight2 >> 1) );

            // Draw box.
            GFX::DrawBox( mRect, mColor, mDrawX );
        }
        // else don't draw
    }
    else // 2D definition
    {
        // Draw box.
        GFX::DrawBox( mRect, mColor, mDrawX );
    }
}

} // namespace hud
